﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;

namespace EasySocket
{
    internal class Tester
    {
        public void Test()
        {
            FixedHeaderPackageAdapter adapter = new FixedHeaderPackageAdapter();

            bool sendCallBack = false;
            bool receivedCallBack = false;

            byte[] sentData = null;
            adapter.SendAsyncCallBack = (sd) =>
            {
                //此处会回调发送的最终调用。例如：此处使用固定包头，则发送的数据为4+n的封装。
                sentData = sd.ToArray();
                if (sd.Length == 4 + 4)
                {
                    sendCallBack = true;
                }
                return Task.CompletedTask;
            };

            adapter.ReceivedAsyncCallBack = (byteBlock, requestInfo) =>
            {
                //此处会回调接收的最终触发，例如：此处使用的固定包头，会解析4+n的数据为n。

                if (byteBlock.Length == 4)
                {
                    receivedCallBack = true;
                }
                return Task.CompletedTask;
            };

            byte[] data = Encoding.UTF8.GetBytes("RRQM");

            adapter.SendInputAsync(data.AsMemory());//模拟输入，会在SendCallBack中输出最终要发送的数据。

            using (ByteBlock block = new ByteBlock())
            {
                block.Write(sentData);
                block.Position = 0;
                adapter.ReceivedInputAsync(block);//模拟输出，会在ReceivedCallBack中输出最终收到的实际数据。
            }
        }
    }
}
